package com.david.rabbitmq.spring.xml;

import org.springframework.amqp.core.*;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import java.io.UnsupportedEncodingException;

/**
 * @author David
 * @version V1.0
 * @Package com.david.rabbitmq.spring.xml
 * @date 2021/7/17 11:32
 */
public class RabbitmqProducerApplication {
    public static void main(String[] args) throws UnsupportedEncodingException {
        AbstractApplicationContext context = new GenericXmlApplicationContext("classpath:/rabbit-context.xml");
        AmqpTemplate template = context.getBean(AmqpTemplate.class);
        Message msg;
        final MessagePropertiesBuilder builder = MessagePropertiesBuilder.newInstance();
        builder.setContentEncoding("utf-8");
        builder.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        for (int i = 0; i < 1000; i++) {
            msg = MessageBuilder.withBody(("你好，世界" + i).getBytes("utf-8"))
                    .andProperties(builder.build())
                    .build();
            template.send("ex.direct", "routing.q1", msg);
            System.out.println("发送消息：你好，世界" + i);
        }
        context.close();
    }
}
